package xin.marcher.module.label.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import xin.marcher.module.label.converter.LabelConverter;
import xin.marcher.module.label.domain.dto.*;
import xin.marcher.module.label.domain.entity.CategoryLabelGroupRelationDO;
import xin.marcher.module.label.domain.entity.FrontCategoryLabelRelationDO;
import xin.marcher.module.label.domain.entity.ProductLabelRelationDO;
import xin.marcher.module.label.domain.request.CategoryLabelGroupRelastionRequest;
import xin.marcher.module.label.domain.request.FrontCategoryLabelRelationRequest;
import xin.marcher.module.label.domain.request.ProductLabelRelationRequest;
import xin.marcher.module.label.mapper.CategoryLabelGroupRelationMapper;
import xin.marcher.module.label.mapper.FrontCategoryLabelGroupRelationMapper;
import xin.marcher.module.label.mapper.ProductLabelRelationMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Repository
public class LabelRelationRepository {

    @Autowired
    private CategoryLabelGroupRelationMapper categoryLabelGroupRelationMapper;

    @Autowired
    private FrontCategoryLabelGroupRelationMapper frontCategoryLabelGroupRelationMapper;

    @Autowired
    private ProductLabelRelationMapper productLabelRelationMapper;

    @Autowired
    private LabelConverter labelConverter;

    public CategoryLabelGroupRelationResultDTO saveCategoryLabelGroupRelation(CategoryLabelGroupRelastionRequest request) {
        CategoryLabelGroupRelationDO categoryLabelGroupRelationDO = labelConverter.convertCategoryLabelGroupRelation(request);
        categoryLabelGroupRelationDO.initCommon();
        categoryLabelGroupRelationMapper.insert(categoryLabelGroupRelationDO);
        return new CategoryLabelGroupRelationResultDTO(true);
    }

    public List<CategoryLabelGroupRelationDTO> listCategoryLabelGroupRelationByCategoryId(Long categoryId) {
        LambdaQueryWrapper<CategoryLabelGroupRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CategoryLabelGroupRelationDO::getCategoryId, categoryId);
        List<CategoryLabelGroupRelationDO> categoryLabelGroupRelationDOS = categoryLabelGroupRelationMapper.selectList(queryWrapper);
        return labelConverter.convertLabelGroupRelationDTOS(categoryLabelGroupRelationDOS);
    }

    public FrontCategoryLabelRelationResultDTO saveFrontCategoryLabelRelation(FrontCategoryLabelRelationRequest request) {
        FrontCategoryLabelRelationDO frontCategoryLabelRelationDO = labelConverter.convertFrontCategoryLabelGroupRelation(request);
        frontCategoryLabelRelationDO.initCommon();
        frontCategoryLabelGroupRelationMapper.insert(frontCategoryLabelRelationDO);
        return new FrontCategoryLabelRelationResultDTO(true);
    }

    public List<FrontCategoryLabelRelationDTO> listFrontCategoryLabelRelationByFrontCategoryId(String frontCategoryId) {
        LambdaQueryWrapper<FrontCategoryLabelRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FrontCategoryLabelRelationDO::getFrontCategoryId, frontCategoryId);
        List<FrontCategoryLabelRelationDO> frontCategoryLabelRelationDTOS = frontCategoryLabelGroupRelationMapper.selectList(queryWrapper);
        return labelConverter.convertFrontLabelGroupRelationDTOS(frontCategoryLabelRelationDTOS);
    }

    public ProductLabelRelationResultDTO saveProductLabelRelation(List<ProductLabelRelationRequest> request) {
        List<ProductLabelRelationDO> productLabelRelationDOS = labelConverter.convertProductLabelRelations(request);
        productLabelRelationDOS.forEach(e -> {
            e.setUpdateUser(request.get(0).getOperateUser());
            e.initCommon();
        });
        productLabelRelationMapper.insertBatch(productLabelRelationDOS);
        return new ProductLabelRelationResultDTO(true);
    }

    public List<ProductLabelRelationDTO> listProductLabels(List<Long> labelIds) {
        LambdaQueryWrapper<ProductLabelRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(ProductLabelRelationDO::getLabelId, labelIds);
        List<ProductLabelRelationDO> productLabelRelationDOS = productLabelRelationMapper.selectList(queryWrapper);
        return labelConverter.convertProductLabelRelationDTOS(productLabelRelationDOS);
    }

    public void removeProductLabelRelation(List<ProductLabelRelationRequest> request) {
        List<String> participantIds = request.stream().map(ProductLabelRelationRequest::getParticipantId).collect(Collectors.toList());
        LambdaQueryWrapper<ProductLabelRelationDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(ProductLabelRelationDO::getParticipantId, participantIds);
        productLabelRelationMapper.delete(queryWrapper);
    }
}
